<?php

include_once('Logging.php');
class DDExpressClass
{

    const APP_KEY_TEST = 'dada49da12778ddac4b';                              # 应用Key，对应开发者账号中的app_key
    const APP_SECRET_TEST = '2d5a37a0ba36548b084aa5820551073e';              # app_secret
    const SOURCE_TEST = '73753';                                             # 商户编号 测试默认
    const SHOPNO_TEST = 11047059;                                              # 门店编号，门店创建后可在门店列表和单页查看 (仅限测试 固定门店编号)【20200326 修改为每家门店对应一个人开发者】

    const URL = 'https://newopen.imdada.cn';         # 地址【正式环境】
    const URL_TEST = 'http://newopen.qa.imdada.cn';  # 地址【测试环境】

    private $isDebug = true;
    private $source_id = '';
    private $app_key = '';
    private $app_secret = '';
    public function __construct($flag,$source_id,$app_key,$app_secret)
    {
        $this->isDebug = $flag;
        $this->source_id = $source_id;
        $this->app_key = $app_key;
        $this->app_secret = $app_secret;
    }


    /**
     * 签名生成
     * @param $parameter  数组参数
     * @return string
     */
    public function _sign($parameter){
        $app_secret = ($this->app_secret);
        # 升序排序
        ksort($parameter);
        # 字符串拼接
        $args = "";
        foreach ($parameter as $key => $value) {
            $args.=$key.$value;
        }
        $args = $app_secret . $args . $app_secret;
        # MD5签名,转为大写
        $sign = strtoupper(md5($args));
        return $sign;
    }


    /**
     *  模拟取消
     * @param $parameter 参数
     */
    public function DDcancel($sendData){
        $app_key = $this->app_key;
        $source_id =  $this->source_id;

        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/cancel';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($sendData,JSON_UNESCAPED_UNICODE),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result = json_decode($result, true);
        }

        return $result;
    }


    /**
     *  模拟接单
     * @param $parameter 参数
     */
    public function DDaccept($sendData){
        $app_key = $this->app_key;
        $source_id =  $this->source_id;

        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/accept';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($sendData,JSON_UNESCAPED_UNICODE),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result = json_decode($result, true);
        }

        return $result;
    }


    /**
     *  模拟取货
     * @param $parameter 参数
     */
    public function DDfetch($sendData){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/fetch';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($sendData,JSON_UNESCAPED_UNICODE),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result = $this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result = json_decode($result, true);
        }
        return $result;
    }

    /**
     *  模拟完成 订单
     * @param $parameter 参数
     */
    public function DDfinish($sendData){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/finish';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($sendData,JSON_UNESCAPED_UNICODE),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }

    /**
     * 获取城市code
     * @param $parameter 参数
     */
    public function getCityCode(){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/cityCode/list';
        $parameter = [
            "app_key" => $app_key,
            "body" => "cityCodeModel",
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 下单   测试会有阶段性请求失败
     * @param $parameter
     * 主 经纬度
     */
    public function send_parameter($sendData){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $jsonParameter['shop_no'] =  (($this->isDebug) ? self::SHOPNO_TEST : $sendData['shop_no']);
        $app_key = $this->app_key;
        $source_id =  $this->source_id;

        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/addOrder';


        $jsonParameter['origin_id'] = $sendData['shop_express_no'];             # 第三方订单ID
        $jsonParameter['city_code'] = $sendData['cityCode'];                    # 订单所在城市的code
        $jsonParameter['cargo_price'] = $sendData['actual_pay'];                # 订单金额
        $jsonParameter['is_prepay'] = 0;                                        # 是否需要垫付 1:是 0:否
        $jsonParameter['receiver_name'] = $sendData['receiver'];                # 收货人姓名
        $jsonParameter['receiver_address'] = $sendData['receiver_address'];     # 收货人地址
        $jsonParameter['cargo_weight'] = $sendData['actual_weight'];            # 订单重量(单位：kg)  （非必填）
        $jsonParameter['cargo_type'] = 5;                                       # 订单商品类型：食品小吃-1,饮料-2,鲜花-3,文印票务-8,便利店-9,水果生鲜-13,同城电商-19, 医药-20,蛋糕-21,酒品-24,小商品市场-25,服装-26,汽修零配-27,数码-28,小龙虾-29, 其他-5 2019-11-20 暂定 5
        $jsonParameter['is_direct_delivery'] = $sendData['is_direct_delivery']; # 是否选择直拿直送（0：不需要；1：需要。选择直拿直送后，同一时间骑士只能配送此订单至完成，同时，也会相应的增加配送费用） 2019-11-20 小龙沟通确认 直拿直送
        $jsonParameter['is_use_insurance'] = 0;                                 # 是否使用保价费（0：不使用保价，1：使用保价； 同时，请确保填写了订单金额（cargo_price））商品保价费(当商品出现损坏，可获取一定金额的赔付) 2019-11-20 小龙沟通确认 不保价
        $jsonParameter['info'] = $sendData['shop_express_remark'];              #  订单备注
        $sta = date('Y-m-d H:i:s',strtotime('+20 minute',time()));  # 大于20分钟
        if($sendData['shop_delivery_time'] > $sta){
            $tstr = substr($sendData['shop_delivery_time'],0,strrpos($sendData['shop_delivery_time'],':'));   # 指定时间 重构
            $jsonParameter['delay_publish_time'] = strtotime($tstr);            # 预约发单时间（预约时间unix时间戳(10位),精确到分;整10分钟为间隔，并且需要至少提前20分钟预约。）  （非必填） 达达API定义
        }

        $jsonParameter['callback'] = $sendData['callbackUrl'].'?md=service&cl=service&at=GetDDCallBack';
        $jsonParameter['receiver_phone'] = $sendData['receiver_tel'];           # 收货人手机号

        # 20200426 判断是否有修改地址 有则用
        if($sendData['slat'] && $sendData['slng']){
            $jsonParameter['receiver_lng'] = $sendData['slng'];
            $jsonParameter['receiver_lat'] = $sendData['slat'];
        }else{
            $jsonParameter['receiver_lng'] = $sendData['lng'];
            $jsonParameter['receiver_lat'] = $sendData['lat'];
        }
        $parameter['app_key'] = $app_key;                                       # 应用Key
        $parameter['body'] = json_encode($jsonParameter);                       # 业务参数
        $parameter['format'] = 'json';                                          # 请求格式
        $parameter['source_id'] = $source_id;                                   # 商户编号
        $parameter['timestamp'] = time();                                       # 时间戳
        $parameter['v'] = '1.0';                                                # API版本
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;                                        # 签名

        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        file_put_contents('logs/abc.log',$result);
        return $result;
    }

    /**
     * 重新下单
     * @param $parameter
     */
    public function re_send_parameter($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/reAddOrder';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }



    /**
     * 查询下单
     * @param $parameter
     */
    public function query($parameters){
        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/status/query';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];

        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }



    /**
     * 查询订单运费后下单
     * @param $parameter
     */
    public function queryDeliverFee($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/queryDeliverFee';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 查询订单运费后下单
     * @param $parameter
     */
    public function addAfterQuery($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/addAfterQuery';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }



    /**
     * 添加小费
     * 1 = 必传
     * 2 = 非必传
     * @param $order_id     ① 第三方订单编号
     * @param $tips         ① 小费金额(精确到小数点后一位，单位：元)
     * @param $city_code    ① 订单城市区号
     * @param $info         ② 备注(字段最大长度：512)
     * @return bool|mixed|string
     */
    public function addTip($order_id,$tips,$city_code,$info){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/addTip';
        $parameters['order_id'] = $order_id;
        $parameters['tips'] = $tips;
        $parameters['city_code'] = $city_code;
        $parameters['info'] = $info;
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 取消原因列表
     * @param $parameter
     */
    public function reasonsReasons(){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/cancel/reasons';
        $parameter = [
            "app_key" => $app_key,
            "body" => "",
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }

    /**
     * 取消订单
     * @param $parameter
     */
    public function formalCancel($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/formalCancel';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 查询追加配送员
     * @param $parameter
     */
    public function transporter(){
        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/appoint/list/transporter';
        $parameters['shop_no'] = $source_id;
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }

    /**
     * 追加订单
     * @param $parameter
     */
    public function exist($parameters){
        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/appoint/exist';
        $parameters['shop_no'] = $source_id;
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }



    /**
     * 取消追加订单
     * @param $parameter
     */
    public function cancelExist($parameters){
        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/appoint/cancel';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }



    /**
     * 投诉原因列表
     * @param $parameter
     */
    public function complaintList(){
        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/complaint/reasons';
        $parameter = [
            "app_key" => $app_key,
            "body" => "",
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 投诉
     * @param $parameter
     */
    public function complaint($parameters){
        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/complaint/dada';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 返投异常确认
     * @param $parameter
     */
    public function confirmError($parameters){
        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/order/confirm/goods';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 注册商户
     * @param $parameter
     */
    public function regMerchant($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;
        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/merchantApi/merchant/add';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => '',
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 添加门店
     * @param $parameter
     */
    public function addShop($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;

        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/shop/add';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
//        echo '<pre>';
//        print_r($parameters);die;
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 修改门店信息
     * @param $parameter
     *
     */
    public function updateShop($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;

        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/shop/update';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
//        echo json_encode($parameters,JSON_UNESCAPED_UNICODE);
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }


    /**
     * 门店信息
     * @param $parameter
     *
     */
    public function detail($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;

        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/shop/detail';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }

    /**
     * 查询余额
     * @param $parameter
     *
     */
    public function balanceQuery($parameters){
//        $app_key = (($this->isDebug) ? self::APP_KEY_TEST : $this->app_key);
//        $source_id =  (($this->isDebug) ? self::SOURCE_TEST : $this->source_id);

        $app_key = $this->app_key;
        $source_id =  $this->source_id;

        $url = (($this->isDebug) ? self::URL_TEST : self::URL).'/api/balance/query';
        $parameter = [
            "app_key" => $app_key,
            "body" => json_encode($parameters),
            "format" => "json",
            "source_id" => $source_id,
            "timestamp" => time(),
            "v" => "1.0",
        ];
        $sign = $this->_sign($parameter);
        $parameter['signature'] = $sign;
        $result =$this->http_post($url, json_encode($parameter,JSON_UNESCAPED_UNICODE), '','json', '','');
        if (!empty($result)){
            $result=json_decode($result, true);
        }
        return $result;
    }



    private function http_post($url, $param, $post_file = false, $contenttype = '', $msg = '')
    {
        $log = new Logging();
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (empty($contenttype)) {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('application/x-www-form-urlencoded; charset=utf-8'));
        } else if ($contenttype == "json") {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8'));

        }

        $strPOST='';
        if (is_string($param)) {
            $strPOST = $param;
        } else {
            $aGET = array();
            foreach ($param as $key => $val) {
                $aGET[] = $key . "=" . ($val);
            }
            $strPOST = join("&", $aGET);
        }

//        //$strPOST=http_build_query($param, '', '&');
//
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($oCurl, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($oCurl, CURLOPT_TIMEOUT, 60);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        curl_setopt($oCurl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        $time=strtotime(date('Y-m-d H:i:s'));
        $log->lfile('dd_express');
        $log->lwrite($time.','.($url));
        if (!empty($msg))$log->lwrite($time.','.$msg);
        $log->lwrite($time.','.$strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);

        $log->lwrite($time.','.($sContent));

        curl_close($oCurl);

        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }


}

?>